/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UsuarioClave.java
 *
 * Created on 03/11/2010, 01:16:54 AM
 */

package pe.edu.upla.lamerced.desk.view;

import javax.swing.JOptionPane;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Users;
import pe.edu.upla.lamerced.service.UsuarioService;

/**
 *
 * @author elvis
 */
public class UsuarioClave extends javax.swing.JInternalFrame {

   private Users usuario;
   private UsuarioService usuarioService;
    /** Creates new form UsuarioClave */
    public UsuarioClave() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnCambiarClave = new javax.swing.JButton();
        btnCancelCambioClave = new javax.swing.JButton();
        txtactual = new javax.swing.JPasswordField();
        txtnueva = new javax.swing.JPasswordField();
        txtconfirm = new javax.swing.JPasswordField();
        lblMensaje = new javax.swing.JLabel();

        setClosable(true);
        setTitle("Cambiar Clave");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Cambiar la clave");

        jLabel2.setText("Clave Actual");

        jLabel3.setText("Clave nueva");

        jLabel4.setText("Confirmar clave nueva");

        btnCambiarClave.setText("Grabar");
        btnCambiarClave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCambiarClaveActionPerformed(evt);
            }
        });

        btnCancelCambioClave.setText("Cancelar");
        btnCancelCambioClave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelCambioClaveActionPerformed(evt);
            }
        });

        lblMensaje.setForeground(new java.awt.Color(204, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(64, 64, 64)
                .addComponent(btnCambiarClave)
                .addGap(43, 43, 43)
                .addComponent(btnCancelCambioClave)
                .addGap(80, 80, 80))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtactual, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                            .addComponent(txtnueva, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                            .addComponent(txtconfirm, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jLabel1)))
                .addGap(29, 29, 29))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtactual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtnueva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtconfirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(lblMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelCambioClave)
                    .addComponent(btnCambiarClave))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelCambioClaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelCambioClaveActionPerformed
       this.dispose();
    }//GEN-LAST:event_btnCancelCambioClaveActionPerformed

    private void btnCambiarClaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCambiarClaveActionPerformed
       //grabar nueva clave
       String actual = "";
       String nueva = "";
       String confirma = "";
       actual = obtenerPassword( txtactual.getPassword() );
       if( !actual.equals( usuario.getPassword() )){
         lblMensaje.setText("su clave actua les incorrecta");
         return;
       }
       nueva = obtenerPassword( txtnueva.getPassword() );
       confirma = obtenerPassword( txtconfirm.getPassword() );
       System.out.println(""+nueva);
       System.out.println(""+confirma);
       if( !nueva.equals( confirma ) ){
         lblMensaje.setText("su clave nueva es icnorrecta");
         return;
       }
       lblMensaje.setText("");
       usuario.setPassword(  confirma );
       usuarioService.modificarUsuario(usuario);
      JOptionPane.showMessageDialog(this, "Clave modficada");
      this.dispose();
    }//GEN-LAST:event_btnCambiarClaveActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCambiarClave;
    private javax.swing.JButton btnCancelCambioClave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lblMensaje;
    private javax.swing.JPasswordField txtactual;
    private javax.swing.JPasswordField txtconfirm;
    private javax.swing.JPasswordField txtnueva;
    // End of variables declaration//GEN-END:variables

   public void cargaInicial(){
      if(usuarioService==null){
         usuarioService = (UsuarioService)Inicial.getFactory().getBean("usuarioService");
      }
   }

   /**
    * @return the usuario
    */
   public Users getUsuario() {
      return usuario;
   }

   /**
    * @param usuario the usuario to set
    */
   public void setUsuario(Users usuario) {
      this.usuario = usuario;
   }

   private String obtenerPassword(char[] password) {
      StringBuilder texto = new StringBuilder();
       for(char car : password ){
            texto.append(car);
         }
      return texto.toString();
   }
   
}
